<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="班级" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="classId">
              <j-dict-select-tag type="list"  v-model="model.classId"  dictCode="sk_class,class_name,id,del_flag = 0 order by sort_no asc" placeholder="请选择班级" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="教师" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="teacherId">
              <a-select v-model="model.teacherId" placeholder="请选择教师">
                <a-select-option v-for="item in teacherList" :key="item.id" :value="item.id">
                  {{ item.realname }}
                </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="checkTime">
              <j-date placeholder="请选择日期" value-format="YYYY-MM-DD" v-model="model.checkTime"  style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="周一" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="week1">
              <a-textarea v-model="model.week1" rows="4" placeholder="请输入周一" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="周二" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="week2">
              <a-textarea v-model="model.week2" rows="4" placeholder="请输入周二" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="周三" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="week3">
              <a-textarea v-model="model.week3" rows="4" placeholder="请输入周三" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="周四" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="week4">
              <a-textarea v-model="model.week4" rows="4" placeholder="请输入周四" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="周五" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="week5">
              <a-textarea v-model="model.week5" rows="4" placeholder="请输入周五" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remark">
              <a-textarea v-model="model.remark" rows="4" placeholder="请输入备注" />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'Tb_1530SafetyEducationTableForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          classId: [
            { required: true, message: '请选择班级!'},
          ],
          teacherId: [
            { required: true, message: '请选择教师!'},
          ],
          checkTime: [
            { required: true, message: '请选择日期!'},
          ],
        },
        url: {
          add: "/table/tb_1530SafetyEducationTable/add",
          edit: "/table/tb_1530SafetyEducationTable/edit",
          queryById: "/table/tb_1530SafetyEducationTable/queryById"
        },
        teacherList:[]
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
      this.getTeacher()
    },
    methods: {
      getTeacher(){
        let url = "/management/teacher/list"
        getAction(url).then((res)=>{
          if(res.success){
            this.teacherList = res.result;
          } else {
            this.$message.warning(res.message);
          }
        }).catch((error)=>{
          console.error(error);
        })
      },
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            this.$emit("modelLoading",that.confirmLoading)
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              this.$emit("modelLoading",that.confirmLoading)
            })
          }
         
        })
      },
    }
  }
</script>